//
//  HJEmoticonTools.swift
//  HJWeiBo
//
//  Created by 夏航军 on 16/10/8.
//  Copyright © 2016年 夏航军. All rights reserved.
//

import UIKit
let KsaveRecentEmoticon = "KsaveRecentEmoticon"
let KSelectEmoticon =  "KSelectEmoticon"

private let path1 = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent("recent.plist")


let emoticonPageCount = 20

class HJEmoticonTools: NSObject {

    //创建单例
    static let sharedEmoticonTools = HJEmoticonTools()
    
    //所有的表情数组
    lazy var allEmoticons : [[[HJEmoticon]]] = {
        
        return [[self.recentEmoticons],
                self.emoticonPages(emoticons: self.defaultEmoticons),
                self.emoticonPages(emoticons:self.emojiEmoticons),
                self.emoticonPages(emoticons:self.LxhEmoticons)]
        
    }()
    
    //将模型数组切割成二维数组
    func emoticonPages(emoticons : [HJEmoticon]) ->[[HJEmoticon]] {
        
        let pageCount = (emoticons.count - 1) / emoticonPageCount + 1
        
        //创建二维数组用于存储截取的数据
        var sectionEmoticon = [[HJEmoticon]]()
        for i in 0..<pageCount {
           
            //截取数组
           let loc = i * emoticonPageCount
           var len = emoticonPageCount
            
            if loc + len > emoticons.count {
                len = emoticons.count - loc
            }
            
            let array = (emoticons as NSArray).subarray(with: NSMakeRange(loc, len))
            sectionEmoticon.append(array as! [HJEmoticon])
        }
        return sectionEmoticon
    }
    
    
    //最近表情
    lazy var recentEmoticons : [HJEmoticon] = {
        if let array = NSKeyedUnarchiver.unarchiveObject(withFile: path1) as? [HJEmoticon] {
            return array
        }
        
        return [HJEmoticon]()
    }()
    
    //默认表情的数组
    lazy var defaultEmoticons : [HJEmoticon] = {
       return self.loadEmoticon(path : "default/info.plist")
    }()
    
    //emoji表情
    lazy var emojiEmoticons : [HJEmoticon] = {
        return self.loadEmoticon(path : "emoji/info.plist")
    }()
    
    //浪小花表情
    lazy var LxhEmoticons : [HJEmoticon] = {
        return self.loadEmoticon(path : "lxh/info.plist")
    }()
    
    //bundle对象的属性
    lazy var emoticonBundle : Bundle = {
        let path = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil)
        
        //根据路劲获取bundle对象
        let emoticonBundle = Bundle.init(path: path!)!
        
        return emoticonBundle
    }()
    
    //根据路径获取对应的模型数组
    func loadEmoticon (path : String) -> [HJEmoticon] {
        
        //使用bundle的pathForResource方法读取bundle对象中的文件
        let infoPath = self.emoticonBundle.path(forResource: path, ofType: nil)
        
        //读取数组类型的plist文件
        let array = NSArray(contentsOfFile: infoPath!) as! [[String : Any]]
        
        //创建数组模型
        var emoticons = [HJEmoticon]()
        
        for item in array {
            
            let emo = HJEmoticon()
            
            emo.yy_modelSet(with: item)
            
            
            //emoticon 没有这个属性
            if let img = emo.png {
                print(emo.imagePath)
               emo.imagePath = (path as NSString).deletingLastPathComponent + "/" + img
            }
            
            emoticons.append(emo)
        }
        
        return emoticons

    }
    
    
    
    //存档
    func saveRecentEmotion(em : HJEmoticon) {
        
        //1.去重,判断最近表情的数组中是否已有该表情
        if recentEmoticons.contains(em) {
            guard let index = recentEmoticons.index(of: em) else {
                return
            }
            //2.如果有则求出索引并且删除该元素
            recentEmoticons.remove(at: index)
        }
        //3.将新的元素添加到数组第一个
        recentEmoticons.insert(em, at: 0)
        
        //4.在存储数组之前需要判断数组的数量是否大于20 如果大于20就应该移除最后一个表情模型
        if recentEmoticons.count > 20 {
            recentEmoticons.removeLast()
        }
        //在swift里面 数组里面添加元素地址会改变,所以要进行重新赋值
        allEmoticons[0][0] = recentEmoticons
        
        //5.存储表情模型数组
        NSKeyedArchiver.archiveRootObject(recentEmoticons, toFile: path1)
        
        //当把点击的图片归档的时候会调用此方法,在此发出通知,当有新数据的时候就刷新最近一组的数据
        //通知的名称  通知的发布者
        NotificationCenter.default.post(name: NSNotification.Name(KsaveRecentEmoticon), object: nil)
        
    }
    
    
}
